//index.js
import api from '../../../../utils/api.js'
//获取应用实例
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: api.imageurl,
    reviewList:[],
    cookId:null,
    dianzan: 'false',
    shoucang: 'false',
  },
  onLoad: function (option) {
    var that = this;
    var cookId = option.cookId
    that.setData({
      cookId: cookId
    })
    // api.getReviewList({
    //   data:{
    //     cook_id: cookId
    //   },
    //   success: function (res) {
    //     console.log(res)
    //     var reviewList = res.data.result;
    //     that.setData({
    //       reviewList: reviewList
    //     })
    //   }
    // })

  },
  onShow:function(){
    var that = this
    api.getReviewList({
      data: {
        cook_id: that.data.cookId
      },
      success: function (res) {
        console.log(res)
        var reviewList = res.data.result;
        that.setData({
          reviewList: reviewList
        })
      }
    })
  },
  // 显示图片
  showImage: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      urls: [url],
    })
  },
  /**
   * 回复评论
   */
  toReply:function(e){
    var reviewId = e.currentTarget.dataset.id;
    var cookId = e.currentTarget.dataset.cookId;
    wx.navigateTo({
      url: '../reply/reply?reviewId=' + reviewId +"&cookId=" +cookId,
    })

  },
  /**
   * 点赞
   */
  addPraise: function () {
    var that = this;
    api.addPraise({
      data: {
        id: that.data.cookId,
        ou_id: app.globalData.userid
      },
      success: function (res) {
        that.setData({
          dianzan: 'true',
          praisenum: ++that.data.praisenum
        })

      }
    })
  },
  /**
 * 取消点赞
 */
  canclePraise: function () {
    var that = this;
    api.canclePraise({
      data: {
        id: that.data.cookId,
        ou_id: app.globalData.userid
      },
      success: function (res) {
        that.setData({
          dianzan: 'false',
          praisenum: --that.data.praisenum
        })
      }
    })
  },
  /**
 * 收藏
 */
  addCollect: function () {
    var that = this;
    api.addCollect({
      data: {
        id: that.data.cookId,
        ou_id: app.globalData.userid
      },
      success: function (res) {
        that.setData({
          shoucang: 'true'
        })

      }
    })
  },
  /**
 * 取消收藏
 */
  cancleCollect: function () {
    var that = this;
    api.cancleCollect({
      data: {
        id: that.data.cookId,
        ou_id: app.globalData.userid
      },
      success: function (res) {
        that.setData({
          shoucang: 'false',
        })

      }
    })
  },
  /**
 * 去发表评论
 */
  toAddReview: function (e) {
    var that = this
    wx.navigateTo({
      url: '../add/add?cookId=' + that.data.cookId
    })
  },
  /**
     * 分享
     */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: "小巴依",
      path: '/pages/cookbook/review/list/list?cookId=' + that.data.cookId 
    }
  },
})